//
//  MyViewController.m
//  BrushFace
//
//  Created by timer_q on 15/12/4.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "MyViewController.h"
#import "MyHeaderView.h"
#import "MyAssetsCell.h"
#import "MyDefaultCell.h"
#import "MyCreditCell.h"
#import "NavHelper.h"
#import "MySceneModel.h"
#import "MeStatus.h"

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray              *_dataArray;
    UIView                      *_navBackView;
    MyHeaderView                *_myHeaderView;
    NSDictionary                *_output;
}
@property (strong, nonatomic)MySceneModel *myModel;
@property (strong, nonatomic)UITableView *tableView;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self UIConfig];
    [self dataInit];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [_myModel user_getMyIndexForAppAndCB:^(id output) {
        _output = output;
        ITLogSP(@"%@",output);
        
        [MeStatus sharedInstance].userId = [output objectForKey:@"userid"];
        if ([[output objectForKey:@"nickname"] isEqual:[NSNull null]] ) {
            [_myHeaderView myViewSetName:@"未填写昵称" andPhoneNum:[output objectForKey:@"registermobile"]];
        }
        else
        {
            [_myHeaderView myViewSetName:[self replayBaseStr:[output objectForKey:@"nickname"]] andPhoneNum:[output objectForKey:@"registermobile"]];
        }
        
        
        
        [_tableView reloadData];
    }];
    
}
- (NSString *)replayBaseStr:(NSString *)nickName{
    NSMutableString *nickN = [[NSMutableString alloc]initWithString:nickName];
    NSMutableString *retStr = [[NSMutableString alloc] init];
    for (int i=0; i<nickN.length; i+=4) {
        NSString *subStr = [NSString stringWithFormat:@"\\u%@",[nickN substringWithRange:NSMakeRange(i, 4)]];
        [retStr appendString:[self replaceUnicode:subStr]];
    }
    return retStr;
}

- (NSString *)replaceUnicode:(NSString *)unicodeStr {
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

#pragma mark - data
- (void)dataInit{
    _myModel = [MySceneModel SceneModel];
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"",@"",
            @{@"title":@"我的借入",@"detail":@"待还0.00",@"image":@"myJieRu"},
            @{@"title":@"我的借出",@"detail":@"待收0.00",@"image":@"myJieChu"},
            @{@"title":@"安全保障",@"detail":@"",@"image":@"mySafe"}, nil];
    [_tableView reloadData];
}

#pragma mark - customUI
- (void)UIConfig{
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TAB_BAR_HEIGHT)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.showsVerticalScrollIndicator = NO;
    _myHeaderView = [MyHeaderView loadBundleNib];
    _myHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*0.66);
    _tableView.tableHeaderView = _myHeaderView;
    [self.view addSubview:_tableView];
    
    _navBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _navBackView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_navBackView];
    
    [self.navBar setTitle:@"我的"];
    [self.navBar getTitleLable].textColor = [UIColor whiteColor];
    [self.navBar setHideLeftBtn:YES];
    [self.navBar setBackgroundColor:[UIColor clearColor]];
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 44)];
    [rightBtn setTitle:@"设置" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(setButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.navBar setRightView:rightBtn];
    [self.view bringSubviewToFront:self.navBar];
    
    
}


#pragma mark - tableViewDelegate and DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 160;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *MyAssetsCellId = @"MyAssetsCell";
    static NSString *MyDefaultCellId = @"MyDefaultCell";
    static NSString *MyCreditCellId = @"MyCreditCell";
    
    
    if (indexPath.section == 0) {
        MyCreditCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCreditCellId];
        if (cell == nil) {
            cell = [MyCreditCell loadBundleNib];
        }
        if (_output!=nil) {
            cell.eduLabel.text = [NSString stringWithFormat:@"%@",[_output objectForKey:@"creditamount"]];
        }

        return cell;
    }else if(indexPath.section == 1){
        MyAssetsCell *cell = [tableView dequeueReusableCellWithIdentifier:MyAssetsCellId];
        if (cell == nil) {
            cell = [MyAssetsCell loadBundleNib];
        }
        if (_output!=nil) {
         [cell cellSetYuE:[NSString stringWithFormat:@"%.2f",[[NSString stringWithFormat:@"%@",[_output objectForKey:@"amount"]] floatValue]] andAll:[NSString stringWithFormat:@"%.2f",[[NSString stringWithFormat:@"%@",[_output objectForKey:@"totalAmount"]] floatValue]] andCoupons:[_output objectForKey:@"countCoupons"] andPeople:[_output objectForKey:@"popularotyamount"]];
        }
        return cell;
    }else{
        MyDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:MyDefaultCellId];
        if (cell == nil) {
            cell = [MyDefaultCell loadBundleNib];
        }

        NSDictionary *defaultDict = [_dataArray objectAtIndex:indexPath.section];
        NSString *title;
        NSString *detail;
        NSString *image;
        title = [defaultDict objectForKey:@"title"];
        detail = [defaultDict objectForKey:@"detail"];
        image = [defaultDict objectForKey:@"image"];
        if (_output!=nil) {
            if (indexPath.section == 2) {
                detail = [NSString stringWithFormat:@"待还%.2f",[[NSString stringWithFormat:@"%@",[_output objectForKey:@"torepayamount"]] floatValue]];
            }else if(indexPath.section ==3){
                detail = [NSString stringWithFormat:@"待收%.2f",[[NSString stringWithFormat:@"%@",[_output objectForKey:@"toincomeamount"]] floatValue]];
            }else if(indexPath.section ==4){
                detail = @"";
            }
        }
        [cell myDefaultCellAddTitle:title andDetail:detail andImage:image];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 2:
        {
            [NavHelper gotoJieruJieChuVCWithType:JIERU_TYPE];
        }
            break;
        case 3:
        {
            [NavHelper gotoJieruJieChuVCWithType:JIECHU_TYPE];
        }
            break;
        case 4:
        {
            [NavHelper gotoSafeVC];
        }
            break;
        case 0:
        {
            [NavHelper gotoCreditLimitVC];
        }
            break;
        default:
            break;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - scrollDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    if (y>0) {
        _navBackView.backgroundColor = [UIColor blackColor];
        _navBackView.alpha=y/100.0;
    }else{
        _navBackView.backgroundColor = [UIColor clearColor];
    }
}
#pragma mark - action
- (void)setButtonClick:(UIButton *)sender{
    [NavHelper gotoSettingVC];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
